package com.zyj.myapplication.Fragment;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.zyj.myapplication.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class HomeFragment extends Fragment {
    private Button activity, coach, match;
    private FrameLayout home_frame;
    private FragmentTransaction transaction;
    private FragmentManager fragmentManager;
    private HomeActivityFragment homeActivityFragmnet;
    private HomeCoachFragment homeCoachFragment;
    private HomeMatchFragment homeMatchFragment;
    private List<ImageView> images;
    private int[] imgResource = {R.drawable.lunbo1, R.drawable.lunbo2, R.drawable.lunbo3, R.drawable.lunbo4};
    private ViewPager home_vp;
    private View view;
    private int current;
    private Context context;
    private int oldpos;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_home, container, false);
        //  recyclerView = (RecyclerView) view.findViewById(R.id.club_rv);
        //  recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));//设置布局
        initViews();
        initEvent();
        lunbo();
        return view;
    }

    private void initEvent() {
        activity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                replace(homeActivityFragmnet);
            }
        });
        coach.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                replace(homeCoachFragment);
            }
        });
        match.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                replace(homeMatchFragment);
            }
        });
    }

    private void lunbo() {
        home_vp.setAdapter(pageadapter);
    }

    public void initViews() {
        activity = (Button) view.findViewById(R.id.activity);
        coach = (Button) view.findViewById(R.id.coach);
        match = (Button) view.findViewById(R.id.match);
        home_frame = (FrameLayout) view.findViewById(R.id.home_frame);
        homeActivityFragmnet = new HomeActivityFragment();
        homeCoachFragment = new HomeCoachFragment();
        homeMatchFragment = new HomeMatchFragment();
        context = getActivity();
        fragmentManager = getChildFragmentManager();
        transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.home_frame, homeActivityFragmnet);
        transaction.commit();
        home_vp = (ViewPager) view.findViewById(R.id.home_vp);

        images = new ArrayList<ImageView>();
        for (int i = 0; i < imgResource.length; i++) {
            ImageView imageView = new ImageView(getActivity());
            imageView.setBackgroundResource(imgResource[i]);
            images.add(imageView);
        }
    }

    PagerAdapter pageadapter = new PagerAdapter() {
        @Override
        public int getCount() {
            return images.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(images.get(position));
            return images.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(images.get(position));
        }
    };

    class PageTask implements Runnable {
        @Override
        public void run() {
            current = (current + 1) % imgResource.length;
            handler.sendEmptyMessage(current);
        }
    }

    private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            home_vp.setCurrentItem(current);
        }

        ;
    };

    @Override
    public void onStart() {
        super.onStart();
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                current = (current + 1) % imgResource.length;
                handler.sendEmptyMessage(current);
            }
        }, 2000, 2000);

    }

    private void replace(Fragment fragment) {
        transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.home_frame, fragment);
        transaction.commit();
    }
}


